jQuery(document).ready(function($) {
   // show a simple loading indicator
   $.metadata.setType("attr", "validate");
   var form_validator = {
      rules: {
         name: {
           remote: {url: validatenameurl, type: "POST"}
         },
         password2: {
            required: true, 
            equalToInForm: "#password"
         }
      },
      messages: {
         password2: {
            equalToInForm: "两次填写的密码不一致"
         }
      },
      failure: function(label) {
         label.removeClass("checked");
      },
      success: function(label) {
         // set &nbsp; as text for IE
         label.addClass("checked");
      },
      submitHandler: function(form) {
         $(form).ajaxSubmit({
            dataType: "json",
            beforeSubmit: function(data, form, options) {
               if(!showLoading(form))
                  return false;
               $("#regmsg", form).hide();
            },
            success: function(data) {
              try {
                  var retmsg = data;
                  if(retmsg.error) {
                     $("#regmsg", form).jobUtil("showError", retmsg.error);
                  }
                  else if(retmsg.success) {
                     $("#regmsg", form).jobUtil("showSuccess", retmsg.success).fadeIn('fast').delay(1000, function() {
                        window.location = jobseekerhomeurl;
                     });
                 }
              } catch(e) {
                 return false;
              }
            }
        });
      }
   };

   $("#jobseekerregform").validate(form_validator);

   $("#jobseekerregform #password").blur(function() {
      if($(this).val().length <= 0)
         return;

      if(!$(this).jobUtil("strengthCheck"))
         $(this).nextAll(".label-warning").fadeIn();
      else
         $(this).nextAll(".label-warning").fadeOut();
   })

   $("#jobseekerregform").jobUtil("showTooltip");

   // focus first text-field 
   setFocus("jobseekerregform");

   $("#jobseekerloginform").jobUtil("setCaptcha", {
      captchaurl: showcaptchaurl
   });

   // login form if it's present
   $.metadata.setType("attr", "validate");
   $("#jobseekerloginform").validate({
      submitHandler: function(form) {
         $(form).ajaxSubmit({
           dataType: "json",
           beforeSubmit: function(data, form, options) {
               if(!showLoading("jobseekerloginform"))
                  return false;
               $("#regmsg").hide();
            },
            success: function(data) {
              try {
                 var retmsg = data;
                 if(retmsg.error) {
                     $(".server", form).jobUtil("showError", retmsg.error);
                     if(typeof(retmsg.data) != 'undefined' && typeof(retmsg.data.captcha) != 'undefined' && retmsg.data.captcha) {
                         $("#logincaptcha #captcha_code").prop("disabled", false); 
                         $("#logincaptcha").fadeIn('fast', function() {
                             if($("#logincaptcha #captcha_code").addClass("required").val().length > 0)
                               $("#logincaptcha #captcha_reload").trigger("click");
                         });
                     }
                     else if(!/captcha_code/.test(retmsg.error)) {
                         if($("#logincaptcha").css("display") != "none")
                             $("#logincaptcha #captcha_reload").trigger("click");
                     }
                 }
                 if(retmsg.success) {
                     window.location = retmsg.redirect;
                  }
              } catch(e) {
                 return false;
              }
            }
         });

         return false;
      }
   });

   $(".submit_btn").prop('disabled',false);

   var keepSessionAlive = function() {
      if (DEVELOPMENT_MODE) return;
      // Wake up every 5 min
      var tick = 1000 * 60 * 5;
      // Use storage/cookie to prevent same request from multiple tabs
      var interview_session_alive = storageGet('interview_session_alive', true);
      var now = (new Date()).getTime();

      if ($.isEmptyObject(interview_session_alive) || interview_session_alive.timestamp <= (now - tick)) {
         $.ajax({
            type: 'GET',
            url: OC.filePath('interview', 'ajax', 'keepalive.php'),
            cache: false,
            success: function(result) {
               storageSet('interview_session_alive', {timestamp: now});
               setTimeout(function() { keepSessionAlive(); }, tick);
            },
            error: function(jqXHR, textStatus) {
               setTimeout(function() { keepSessionAlive(); }, tick);
            }
         });
      } else {
         setTimeout(function() { keepSessionAlive(); }, tick);
      }
   };

   keepSessionAlive();

});